package com.yuexun.beilunpatient.ui.registration.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.family.ui.Act_Family_List;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentSubmitBean;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentTimeSlotBean;
import com.yuexun.beilunpatient.ui.registration.bean.ArrageViewData;
import com.yuexun.beilunpatient.ui.registration.model.impl.RegistrationModel;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentTimeSlotPresenter;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentTimeSlotView;
import com.yuexun.beilunpatient.utils.DateUtil;
import com.yuexun.beilunpatient.widget.ActionSheetDialog;
import com.yuexun.beilunpatient.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/10/18.
 */
public class Act_Registration_Submit extends BaseKJActivity implements IAppointmentTimeSlotView {

    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_idcard)
    TextView tvIdcard;
    @Bind(R.id.tv_department)
    TextView tvDepartment;
    @Bind(R.id.doc_name)
    TextView docName;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.order_time)
    TextView orderTime;
    @Bind(R.id.tv_fee)
    TextView tvFee;

    private String docname;
    private String docdep;
    ArrageViewData registrationInfo;
    AppointmentTimeSlotPresenter slotPresenter;

    List<AppointmentTimeSlotBean> slotBeans = new ArrayList<>();
    String yysjd = "",yysjd_num = "";
    LoadingDialog dialog;

    String member_num_proxy;
    int first = 0;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 10000:
                tvName.setText(data.getExtras().getString("name"));
                tvIdcard.setText(data.getExtras().getString("idCard"));
                member_num_proxy = data.getExtras().getString("memberNum");
//                toAppointment();
                break;
            default:
                break;
        }
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.act_registration_submit);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        slotPresenter = new AppointmentTimeSlotPresenter(new RegistrationModel(), this);
        docname = getIntent().getExtras().getString("docname");
        docdep = getIntent().getExtras().getString("docdep");
        registrationInfo = (ArrageViewData) getIntent().getExtras().getSerializable("registrationInfo");

        getTimeSlot();

        dialog = new LoadingDialog(aty);
        dialog.show();

        tvName.setText(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_NAME));
        tvIdcard.setText(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ID_CARD));
        tvDepartment.setText(docdep);
        docName.setText(docname);
        try {
            String week = DateUtil.getDayOfWeek(new SimpleDateFormat("yyyy-MM-dd").parse(registrationInfo.getSchedule_date()));
            tvDate.setText(registrationInfo.getSchedule_date() + " " + week);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(registrationInfo.getOut_time().equals("A")){
            tvTime.setText("上午");
        }else if(registrationInfo.getOut_time().equals("P")){
            tvTime.setText("下午");
        }else if(registrationInfo.getOut_time().equals("F")){
            tvTime.setText("全天");
        }else if(registrationInfo.getOut_time().equals("N")){
            tvTime.setText("夜间");
        }
        tvFee.setText(registrationInfo.getReg_fee());

        member_num_proxy = PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.MEMBENUM);
    }

    public void getTimeSlot(){
        Map<String, String> params = new HashMap<>();
        params.put("scheduleNum", registrationInfo.getSchedule_num());
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        slotPresenter.getAppointmentTimeSlot(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back, R.id.time_select, R.id.submit, R.id.rl_self})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.time_select:
                first = 1;
                dialog.show();
                getTimeSlot();
                break;
            case R.id.submit:
                toAppointment();
                break;
            case R.id.rl_self:
                Bundle bundle = new Bundle();
                Intent intent = new Intent(this, Act_Family_List.class);
                bundle.putString("type", "2");
                intent.putExtras(bundle);
                aty.startActivityForResult(intent, 1);
                break;
        }
    }

    private void toAppointment(){
        Map<String, String> params = new HashMap<>();
        params.put("schedule_num", registrationInfo.getSchedule_num());
        params.put("member_num_proxy", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.MEMBENUM));
        params.put("access_token", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCESSTOKEN));
        params.put("client_ip", "192.168.1.1");
        params.put("member_num", member_num_proxy);
        params.put("yysjd", yysjd);
        params.put("yysjd_num", yysjd_num);
        params.put("ghyzm", "");
        params.put("sfdxzf", "1");//是否采用短信支付 0 采用 1 不采用 默认 0
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        slotPresenter.toAppointment(params);

        dialog.show();
    }

    /**
     * 选择时间
     */
    private void selectTimeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(aty).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);

        for (int i = 0; i < slotBeans.size(); i++) {
            final int final1 = i;
            actionSheetDialog.addSheetItem(slotBeans.get(i).getYysjd_num() + "号" + slotBeans.get(i).getYysjd(), ActionSheetDialog.SheetItemColor.Blue,
                    new ActionSheetDialog.OnSheetItemClickListener() {
                        @Override
                        public void onClick(int which) {
                            yysjd_num = slotBeans.get(final1).getYysjd_num();
                            yysjd = slotBeans.get(final1).getYysjd();
                            orderTime.setText(yysjd_num + "号 " + yysjd);
                        }
                    });
        }
        actionSheetDialog.show();
    }

    @Override
    public void getAppointmentTimeSlot(BaseEntity<AppointmentTimeSlotBean> baseEntity) {
        dialog.dismiss();
        if(baseEntity.getStatus().equals("200")){
            slotBeans.clear();
            if(baseEntity.getDatas().size() > 0){
                for(AppointmentTimeSlotBean bean : baseEntity.getDatas()){
                    if(bean.getYysjd_sfyy().equals("0")){
                        slotBeans.add(bean);
                    }
                }

                if(slotBeans.size()>0){
                    yysjd_num = slotBeans.get(0).getYysjd_num();
                    yysjd = slotBeans.get(0).getYysjd();
                    orderTime.setText(yysjd_num + "号 " + yysjd);
                }
                if(first == 1){
                    selectTimeDialog();
                }

            }
        }
    }

    @Override
    public void toAppointment(BaseEntity<AppointmentSubmitBean> baseEntity) {
        dialog.dismiss();
        if(baseEntity.getStatus().equals("200")){
            ViewInject.toast("挂号成功");
            PreferenceHelper.write(aty, AppConfig.USER_INFO, AppConfig.MEMBENUMPROXY, member_num_proxy);
            Bundle bundle = new Bundle();
            bundle.putString("scheduleGhxh", baseEntity.getDatas().get(0).getSchedule_ghxh());
            bundle.putString("scheduleDate", registrationInfo.getSchedule_date());
            Intent intent_order = new Intent(aty, Act_Registration_Detail.class);
            intent_order.putExtras(bundle);
            startActivity(intent_order);
            finish();
        }else{
            ViewInject.toast(baseEntity.getMessage());
        }
    }

}
